package com.hecore.lagou.speak.entity;

import org.apache.hadoop.io.Writable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * @Author: hecore
 * @Date: 2020/10/18 19:15
 * @Description: 实现序列化接口的实体类,实现序列化的目的是为了反序列化复制对象信息
 */

public class SpeakBean implements Writable {

    /**
     * 自有内容
     */
    private long selfDuration;

    private long thirdPartDuration;

    private String deviceId;

    // 总时长
    private long sumDuration;

    //2 反序列化时，需要反射调⽤空参构造函数，所以必须有
    public SpeakBean() {
    }

    // 3.
    public SpeakBean(long selfDuration, long thirdPartDuration,String deviceId) {
        this.selfDuration = selfDuration;
        this.thirdPartDuration = thirdPartDuration;
        this.sumDuration=this.selfDuration+this.thirdPartDuration;
        this.deviceId=deviceId;
    }

    //序列化方法,吧内容输出到网络/文本中

    /**
     * writeUTF是啥?  写入字符串，乱码
     * @param output
     * @throws IOException
     */
    public void write(DataOutput output) throws IOException {
        output.writeLong(selfDuration);
        output.writeLong(thirdPartDuration);
        output.writeUTF(deviceId);
        output.writeLong(sumDuration);
    }

    /**
     * 反序列化方法
     * @param input
     * @throws IOException
     */
    public void readFields(DataInput input) throws IOException {
        this.selfDuration=input.readLong();
        this.thirdPartDuration=input.readLong();
        this.deviceId=input.readUTF();
        this.sumDuration=input.readLong();
    }

    // 6 编写toString⽅法，⽅便后续打印到⽂本
    @Override
    public String toString() {
        return selfDuration +
                "\t" + thirdPartDuration+
                "\t" + deviceId+
                "\t" + sumDuration ;
    }

    public void set(long selfDuration, long thirdPartDuration,String deviceId) {
        this.selfDuration = selfDuration;
        this.thirdPartDuration = thirdPartDuration;
        this.deviceId=deviceId;
        this.sumDuration=this.selfDuration+this.thirdPartDuration;
    }

    public long getSelfDuration() {
        return selfDuration;
    }

    public void setSelfDuration(long selfDuration) {
        this.selfDuration = selfDuration;
    }

    public long getThirdPartDuration() {
        return thirdPartDuration;
    }

    public void setThirdPartDuration(long thirdPartDuration) {
        this.thirdPartDuration = thirdPartDuration;
    }

    public String getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }

    public long getSumDuration() {
        return sumDuration;
    }

    public void setSumDuration(long sumDuration) {
        this.sumDuration = sumDuration;
    }
}
